package com.longdaji.decoration.ui.shippingaddr;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.data.bean.AddressBean;
import com.longdaji.decoration.ui.activitiesOfMine.receivingAddress.ReceivingAddressActivity;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.widget.CommonDialog;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by longtao on 2017/12/19.
 */

public class ShippingAddrActivity extends BaseActivity implements ShippingAddrContract.View, SwipeRefreshLayout.OnRefreshListener {

    private static final String TAG = ShippingAddrActivity.class.getSimpleName();
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.recycler_view_addr)
    RecyclerView mRecyclerView;
    @BindView(R.id.tv_no_data)
    TextView mTvNoData;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout mSwipeRefresh;
    @BindView(R.id.btn_add_addr)
    Button mAddAddr;

    @Inject
    ShippingAddrPresenter mPresenter;
    private AddrsAdapter mAdapter;


    public static Intent getStartIntent(Context context) {
        Intent intent = new Intent(context, ShippingAddrActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "ShippingAddrActivity onCreate");
        setContentView(R.layout.activity_shipping_addr);
        setUnBinder(ButterKnife.bind(this));
        initView();
    }

    private void initView() {
        if (mPresenter != null) {
            mPresenter.setView(this);
        }
        mTvTitle.setText("管理收货地址");
        initSwipe();
        initReclerView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void initSwipe() {
        // 设置颜色属性的时候一定要注意是引用了资源文件还是直接设置16进制的颜色，因为都是int值容易搞混
        // 设置下拉进度的背景颜色，默认就是白色的
        mSwipeRefresh.setProgressBackgroundColorSchemeResource(android.R.color.white);
        // 设置下拉进度的主题颜色
        mSwipeRefresh.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark);
        // 下拉时触发SwipeRefreshLayout的下拉动画，动画完毕之后就会回调这个方法
        mSwipeRefresh.setOnRefreshListener(this);
    }

    private void initReclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new AddrsAdapter(this, new ArrayList<AddressBean.Address>(), mItemListener);
        mRecyclerView.setAdapter(mAdapter);

    }

    private void getData() {
        mPresenter.getAddrsList(Account.getInstance().getUserid());
    }

    @Override
    protected void onDestroy() {
        mPresenter.detachView();
        super.onDestroy();
    }

    @OnClick({R.id.iv_back, R.id.btn_add_addr})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_add_addr:
                Intent intent = ReceivingAddressActivity.getStartIntent(ShippingAddrActivity.this);
                startActivity(intent);
                break;
            default:
                break;
        }
    }


    @Override
    public void onRefresh() {
        mSwipeRefresh.setRefreshing(true);
        mPresenter.getAddrsList(Account.getInstance().getUserid());
    }

    private CommonDialog selfDialog;
    private AddrsAdapter.AddrItemListener mItemListener = new AddrsAdapter.AddrItemListener() {

        @Override
        public void onEditAddrItem(AddressBean.Address addrItem) {
            LogUtil.d(TAG, "addrItem addr=" + addrItem.getProvince() + addrItem.getCity());
            Intent intent = ReceivingAddressActivity.getStartIntent(ShippingAddrActivity.this);
            intent.putExtra("addr_bean", addrItem);
            startActivity(intent);

        }

        @Override
        public void onDeleteAddrItem(final AddressBean.Address addrItem) {
            selfDialog = new CommonDialog(ShippingAddrActivity.this);
            selfDialog.setTitle("确定要删除该地址吗?");
            selfDialog.setYesOnclickListener("确定", new CommonDialog.onYesOnclickListener() {
                @Override
                public void onYesClick() {
                    mPresenter.deleteCurAddr(addrItem.getUserid(), addrItem.getShippingId());
                    selfDialog.dismiss();
                }
            });
            selfDialog.setNoOnclickListener("取消", new CommonDialog.onNoOnclickListener() {
                @Override
                public void onNoClick() {
                    selfDialog.dismiss();
                }
            });
            selfDialog.show();

        }

        @Override
        public void setDefaultAddr(final AddressBean.Address addrItem) {
            mPresenter.setDefaultAddr(addrItem.getUserid(), "1", addrItem.getShippingId());
        }
    };

    @Override
    public void showAddrsList(List<AddressBean.Address> addressList) {
        mSwipeRefresh.setRefreshing(false);
        mTvNoData.setVisibility(View.GONE);
        mRecyclerView.setVisibility(View.VISIBLE);
        mAdapter.setData(addressList);

    }

    @Override
    public void showNoData() {
        mSwipeRefresh.setRefreshing(false);
        mTvNoData.setVisibility(View.VISIBLE);
        mRecyclerView.setVisibility(View.GONE);
    }

    @Override
    public void showDeleteAddrSuccess() {
        toast("删除地址成功");
        mSwipeRefresh.setRefreshing(true);
        mPresenter.getAddrsList(Account.getInstance().getUserid());
    }

    @Override
    public void showDeleteAddrFail() {
        toast("删除地址失败");
    }

    @Override
    public void setDefaultAddrSuccess() {
        toast("设置默认地址成功");
        mSwipeRefresh.setRefreshing(true);
        mPresenter.getAddrsList(Account.getInstance().getUserid());
    }

    @Override
    public void setDefaultAddrFail() {
        toast("设置默认地址失败");
    }
}
